﻿using UnityEditor;
using UnityEngine;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Xml;

public class AssetBundleController : EditorWindow
{
    public static AssetBundleController window;
    public static UnityEditor.BuildTarget buildTarget = BuildTarget.StandaloneWindows;

    [MenuItem("DXWEditor/BuildAssetBundle/AssetBundle For Windows32", false, 1)]
    public static void ExecuteWindows32()
    {
        if (window == null)
        {
            window = (AssetBundleController)GetWindow(typeof(AssetBundleController));
        }
        buildTarget = UnityEditor.BuildTarget.StandaloneWindows;
        window.Show();
    }

    [MenuItem("DXWEditor/BuildAssetBundle/AssetBundle For IPhone", false, 2)]
    public static void ExecuteIPhone()
    {
        if (window == null)
        {
            window = (AssetBundleController)GetWindow(typeof(AssetBundleController));
        }
        buildTarget = UnityEditor.BuildTarget.iPhone;
        window.Show();
    }

    [MenuItem("DXWEditor/BuildAssetBundle/AssetBundle For Mac", false, 3)]
    public static void ExecuteMac()
    {
        if (window == null)
        {
            window = (AssetBundleController)GetWindow(typeof(AssetBundleController));
        }
        buildTarget = UnityEditor.BuildTarget.StandaloneOSXUniversal;
        window.Show();
    }

    [MenuItem("DXWEditor/BuildAssetBundle/AssetBundle For Android", false, 4)]
    public static void ExecuteAndroid()
    {
        if (window == null)
        {
            window = (AssetBundleController)GetWindow(typeof(AssetBundleController));
        }
        buildTarget = UnityEditor.BuildTarget.Android;
        window.Show();
    }

    [MenuItem("DXWEditor/BuildAssetBundle/AssetBundle For WebPlayer", false, 5)]
    public static void ExecuteWebPlayer()
    {
        if (window == null)
        {
            window = (AssetBundleController)GetWindow(typeof(AssetBundleController));
        }
        buildTarget = UnityEditor.BuildTarget.WebPlayer;
        window.Show();
    }

    private static void MoveFileWithMD5Name(string filePath, bool isTreeMenu = true)
    {
        DirectoryInfo dirinfo = new DirectoryInfo(filePath);
        if (Tools.FileManager.deleteDirs(dirinfo))
        {
            AssetDatabase.Refresh();
            List<string> list = new List<string>();
            Dictionary<string, string> oldpathlist = new Dictionary<string, string>();
            oldpathlist = Tools.FileManager.GetDirs(Application.dataPath + "/AssetBundlePackage", ref list, ".assetbundle", oldpathlist);
            //添加平台文件 filePath+平台
            string filePathTemp = null;
            foreach (var item in list)
            {
                filePathTemp = filePath;
                var path = item.Substring(item.IndexOf("AssetBundle\\") + 12);//获得平台
                path = path.Substring(0, path.IndexOf('.') - 7);
                filePathTemp = Path.Combine(filePathTemp, path);
                Tools.FileManager.moveDirectioryFileToOtherWithNameChangeToMD5(item, filePathTemp, isTreeMenu);
            }

        }
        AssetDatabase.Refresh();
        EditorUtility.DisplayDialog("", "Move Completed", "OK");
    }



    [MenuItem("DXWEditor/[2]MoveALLABToMD5FileWithNameChange[Client]", false, 6)]
    public static void MoveFileWithMD5NameClient()
    {
        var filePath = Application.dataPath + "/archive";
        MoveFileWithMD5Name(filePath);
    }

    [MenuItem("DXWEditor/[2]MoveALLABToMD5FileWithNameChange[Server]", false, 7)]
    public static void MoveFileWithMD5NameServer()
    {
        var filePath = Application.dataPath + "/archiveServer";
        MoveFileWithMD5Name(filePath, false);
    }

    private static void xmlMove(string filePath, string archiveName)
    {
        List<string> filePathList = new List<string>();
        Tools.FileManager.getFilesInSomePath(filePath, ".xml", ref filePathList);
        foreach (var path in filePathList)
        {
            var newpath = path.Substring(path.IndexOf("AssetBundle\\") + 12);
            newpath = newpath.Replace('\\', '/');
            newpath = newpath.Substring(0, newpath.IndexOf('/'));
            newpath = Path.Combine(Application.dataPath + archiveName + "/" + newpath, "flist.base.xml"); //修改xml的名字
            Tools.FileManager.fileCopy(path, newpath);
        }
        AssetDatabase.Refresh();
        EditorUtility.DisplayDialog("", "Move Completed", "OK");
    }

    [MenuItem("DXWEditor/[3]GetSrouceXML[Client]", false, 8)]
    public static void MoveXMLClient()
    {
        var filePath = Application.dataPath + "/AssetBundlePackage/";
        xmlMove(filePath, "/archive");
    }

    [MenuItem("DXWEditor/[3]GetSourceXML[Server]", false, 9)]
    public static void MoveXMLServer()
    {
        var filePath = Application.dataPath + "/AssetBundlePackage/";
        xmlMove(filePath, "/archiveServer");
    }


    void OnGUI()
    {
        if (GUI.Button(new Rect(10f, 10f, 200f, 50f), "[1]CreateAssetBundle"))
        {
            CreateAssetBundle.Execute(buildTarget);
            EditorUtility.DisplayDialog("", "Step (1) Completed", "OK");
        }

        if (GUI.Button(new Rect(10f, 80f, 200f, 50f), "[2]Generate MD5"))
        {
            CreateMD5List.Execute(buildTarget);
            EditorUtility.DisplayDialog("", "Step (2) Completed", "OK");
        }

        if (GUI.Button(new Rect(10f, 150f, 200f, 50f), "[3]Compare MD5"))
        {
            CampareMD5ToGenerateVersionNum.Execute(buildTarget);
            EditorUtility.DisplayDialog("", "Step (3) Completed", "OK");
        }

        if (GUI.Button(new Rect(10f, 220f, 200f, 50f), "[4]Build VersionNum.xml"))
        {
            CreateAssetBundleForXmlVersion.Execute(buildTarget);
            EditorUtility.DisplayDialog("", "Step (4) Completed", "OK");
        }
    }

    public static string GetPlatformPath(UnityEditor.BuildTarget target)
    {
        string SavePath = "Assets/AssetBundlePackage/AssetBundle/";
        switch (target)
        {
            case BuildTarget.StandaloneWindows:
                SavePath += "Windows32/";
                break;
            case BuildTarget.StandaloneWindows64:
                SavePath += "Windows64/";
                break;
            case BuildTarget.iPhone:
                SavePath += "IOS/";
                break;
            case BuildTarget.StandaloneOSXUniversal:
                SavePath += "Mac/";
                break;
            case BuildTarget.Android:
                SavePath += "Android/";
                break;
            case BuildTarget.WebPlayer:
                SavePath += "WebPlayer/";
                break;
            default:
                SavePath += "default/";
                break;
        }

        if (Directory.Exists(SavePath) == false)
            Directory.CreateDirectory(SavePath);

        return SavePath;
    }

    public static string GetPlatformName(UnityEditor.BuildTarget target)
    {
        string platform = "Windows32";
        switch (target)
        {
            case BuildTarget.StandaloneWindows:
                platform = "Windows32";
                break;
            case BuildTarget.StandaloneWindows64:
                platform = "Windows64";
                break;
            case BuildTarget.iPhone:
                platform = "IOS";
                break;
            case BuildTarget.StandaloneOSXUniversal:
                platform = "Mac";
                break;
            case BuildTarget.Android:
                platform = "Android";
                break;
            case BuildTarget.WebPlayer:
                platform = "WebPlayer";
                break;
            default:
                break;
        }
        return platform;
    }
}